package com.wzx.data.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.data.dao.mapper.CollectionMapper;
import com.wzx.data.entity.Collection;
import com.wzx.data.entity.Course;
import com.wzx.data.service.ICollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzx.data.service.ICourseService;
import com.wzx.data.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收藏 服务层接口实现
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@Service
@Transactional
public class ICollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {


    @Autowired
    private ICourseService iCourseService;
    @Override
    public PageInfo<Course> getListData(Collection collection, PageVo pageVo) {
        //根据用户Id获取收藏列表
        QueryWrapper<Collection> qw = new QueryWrapper<>();
        if(!NullUtils.isNull(collection.getUserId())){
            qw.eq("user_id", collection.getUserId());
        }
        if(!NullUtils.isNull(collection.getFolder_id())){
            qw.eq("folder_id", collection.getFolder_id());
        }
        List<Collection> idList = this.list(qw);
        List<Course> courseList = null;
        //根据收藏列表的课程id获取课程数据
        for(Collection data:idList){
            Course courseById = iCourseService.getById(data.getCourseId());
            courseList.add(courseById);
        }
        //根据条件获取全部课程列表
        LambdaQueryWrapper<Course> courseLambda = new LambdaQueryWrapper<>();
        if(!NullUtils.isNull(collection.getKeyword())){
            courseLambda.like(Course::getTitle,collection.getKeyword());
        }
        IPage<Course> data = iCourseService.page(PageUtil.initMpPage(pageVo),courseLambda);
        List<Course> records = data.getRecords();
        //取出交集
        courseList.retainAll(records);

        PageInfo<Course> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal(courseList.size());
        pageInfo.setTotalList(courseList);
        return pageInfo;
    }
}